package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyClubInfoDao;
import com.game.server.db.dao.XyUnionInfoDao;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.message.proto.in.AbstractXYLeaveUnionReq;
import com.game.server.message.proto.out.XYInformDataAlterRsp;
import com.game.server.message.proto.out.XYLeaveUnionRsp;

public class XYLeaveUnionReq extends AbstractXYLeaveUnionReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
            XYLeaveUnionRsp rsp = new XYLeaveUnionRsp();
            rsp.setIsok(1);
            if (client.getClubid() == this.clubid){
                XyClubInfo xyClubInfo = DataManager.getInstance().getMapClub().get(this.clubid);
                if (xyClubInfo!=null && xyClubInfo.getCreatorid() == client.getUserId()){
                    XyUnionInfo unionInfo = DataManager.getInstance().getMapUnion().get(xyClubInfo.getUnionid());
                    if (unionInfo!=null && xyClubInfo.getUnionid()>0 && unionInfo.getCreator_clubid()!=xyClubInfo.getId()){

                        XYInformDataAlterRsp alterRsp = new XYInformDataAlterRsp();
                        alterRsp.setType(1);
                        alterRsp.setOperateclubid(this.clubid);
                        alterRsp.setClubid(this.clubid);
                        alterRsp.setUnionid(unionInfo.getId());
                        unionInfo.broadCastAdminPlayer(alterRsp);

                        XyClubInfoDao.updateUnionId(xyClubInfo.getId(),0);
                        xyClubInfo.setUnionid(0);
                        unionInfo.getClubids().remove(Integer.valueOf(xyClubInfo.getId()));
                        rsp.setIsok(0);
                    }
                }
            }
            client.Send(rsp);
        }
    }

}
